package com.dingding.gmall.ware.controller;

import com.dingding.gmall.common.exception.BizCodeEnum;
import com.dingding.gmall.common.exception.NoStockException;
import com.dingding.gmall.common.utils.PageUtils;
import com.dingding.gmall.common.utils.R;
import com.dingding.gmall.ware.dto.WareSkuLockDTO;
import com.dingding.gmall.ware.entity.WareSkuEntity;
import com.dingding.gmall.ware.service.WareSkuService;
import com.dingding.gmall.ware.vo.SkuHasStockVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品库存
 *
 * @author liudingding
 * @email sunlightcs@gmail.com
 * @date 2020-07-02 22:56:09
 */
@Slf4j
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;


    @PostMapping("/lock/order")
    public R orderLockStock(@RequestBody WareSkuLockDTO wareSkuLockDTO) {
        try {
            wareSkuService.orderLockStock(wareSkuLockDTO);
            return R.ok();
        } catch (NoStockException e) {
            log.info(BizCodeEnum.NO_STOCK_EXCEPTION.getMsg());
            return R.error(BizCodeEnum.NO_STOCK_EXCEPTION.getCode(), BizCodeEnum.NO_STOCK_EXCEPTION.getMsg());
        }
    }

    //查询是否有库存
    @PostMapping("hasStock")
    public R getSkusHasStock(@RequestBody List<Long> skuIds){
        List<SkuHasStockVO> vos = wareSkuService.getSkusHasStock(skuIds);
        return R.ok().put("data",vos);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
